<?php

namespace App\Admin\Controllers;

use App\Models\UserHealth2;
use Carbon\Carbon;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\DB;

class UserHealth2Controller extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserHealth2(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user_id');
            $grid->column('history_chd');
            $grid->column('history_ap');
            $grid->column('history_htn');
            $grid->column('history_dm');
            $grid->column('history_bih');
            $grid->column('history_sss');
            $grid->column('history_cso');
            $grid->column('history_lrp');
            $grid->column('history_gout');
            $grid->column('history_ra');
            $grid->column('history_cg');
            $grid->column('history_other');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserHealth2(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('history_chd');
            $show->field('history_ap');
            $show->field('history_htn');
            $show->field('history_dm');
            $show->field('history_bih');
            $show->field('history_sss');
            $show->field('history_cso');
            $show->field('history_lrp');
            $show->field('history_gout');
            $show->field('history_ra');
            $show->field('history_cg');
            $show->field('history_other');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserHealth2(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('history_chd');
            $form->text('history_ap');
            $form->text('history_htn');
            $form->text('history_dm');
            $form->text('history_bih');
            $form->text('history_sss');
            $form->text('history_cso');
            $form->text('history_lrp');
            $form->text('history_gout');
            $form->text('history_ra');
            $form->text('history_cg');
            $form->text('history_other');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function userHealth(Content $content, Request $request)
    {
        $userId = $request->input('user_id');
        $user = User::find($userId);
        $age = '';
        if (!empty($user->birthday)) {
            $birthday = Carbon::createFromDate($user->birthday);
            $today = Carbon::now();
            $age = $today->diffInYears($birthday);
        }

        $form = UserHealth2::where('user_id', $userId)
            ->get()->first();

        $content->title('健康管理档案');
        $content->description('');
        return $content->body(admin_view('health_form_2', compact('form', 'userId', 'user', 'age')));
    }

    public function addUserHealth(Content $content, Request $request)
    {
        $formId = $request->input('form_id');

        if (!empty($formId)) {
            $form = UserHealth2::find($formId);
        } else {
            $form = new UserHealth2();
        }

        DB::beginTransaction();
        try {

            $data = $request->except(['_token', 'form_id']);
            $data = array_filter($data);
            $form->setRawAttributes($data);
            $form->save();

            DB::commit();

            $content->withSuccess('保存成功');
            return redirect()->back();

        } catch (\Exception $e) {
            DB::rollBack();
            $content->withError($e->getMessage());
            return redirect()->back();
        }


    }
}
